package com.example.netty.service.offside;

import com.example.netty.common.R;
import com.example.netty.pojo.Offside;
import com.example.netty.vo.OffsideVo;
import com.influxdb.client.InfluxDBClient;
import com.influxdb.client.QueryApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

@Service
public class OffsideServiceImpl  {
    @Autowired
    InfluxDBClient influxDBClient;

//    public R getList(OffsideVo offsideVo) throws ParseException {
//        QueryApi queryApi = influxDBClient.getQueryApi();
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        Date startTime = sdf.parse(offsideVo.getCreateTime());
//        Date stopTime = sdf.parse(offsideVo.getCreateTime());
//        stopTime.setTime(stopTime.getTime() + 59 * 1000);
//        SimpleDateFormat utcsdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
//        utcsdf.setTimeZone(TimeZone.getTimeZone("UTC"));
//        String start = utcsdf.format(startTime);
//        String stop = utcsdf.format(stopTime);
//        String flux =String.format("from(bucket: \"my-first\")\n" +
//                "  |> range(start: %s, stop: %s)\n" +
//                "  |> filter(fn: (r) => r[\"_measurement\"] == \"Offside\")\n" +
//                "  |> group(columns: [\"_measurement\",\"_field\"], mode:\"by\")\n" +
//                "  |> sort(columns: [\"_time\",\"keyid\"], desc: true)\n" +
//                "  |> limit(n:%s, offset: %s)\n" +
//                "  |> pivot(rowKey:[\"_time\",\"keyid\"], columnKey: [\"_field\"], valueColumn: \"_value\")\n" +
//                "  |> sort(columns: [\"keyid\"])",start,stop,offsideVo.getPageSize(),(offsideVo.getPageNo()-1)*offsideVo.getPageSize());
//        List<Offside> list=queryApi.query(flux,Offside.class);
//        return R.ok().put("offsideIPage",list);
//    }

    public List<Offside> eightList(String bucketName) {
        QueryApi queryApi = influxDBClient.getQueryApi();
        String flux =String.format("from(bucket:\"%s\")\n" +
                "  |> range(start: -24h)\n" +
                "  |> filter(fn: (r) => r[\"_measurement\"] == \"Offside\")\n" +
                "  |> group(columns: [\"_measurement\",\"_field\"], mode:\"by\")\n" +
                "  |> sort(columns: [\"_time\",\"keyid\"], desc: true)\n" +
                "  |> limit(n:8)\n" +
                "  |> pivot(rowKey:[\"_time\",\"keyid\"], columnKey: [\"_field\"], valueColumn: \"_value\")",bucketName);
        return queryApi.query(flux,Offside.class);
    }

    public List<Offside> newList(String bucketName) {
        QueryApi queryApi = influxDBClient.getQueryApi();
        String flux =String.format("from(bucket: \"%s\")\n" +
                "  |> range(start:-24h)\n" +
                "  |> filter(fn: (r) => r[\"_measurement\"] == \"Offside\")\n" +
                "  |> last()\n" +
                "  |> pivot(rowKey:[\"_time\"], columnKey: [\"_field\"], valueColumn: \"_value\")",bucketName);
        return queryApi.query(flux,Offside.class);
    }

    public List<Offside> eightListData(String bucketName,String time) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date startTime = sdf.parse(time);
        Date stopTime = sdf.parse(time);
        stopTime.setTime(stopTime.getTime() + 59 * 1000);
        SimpleDateFormat utcsdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
        utcsdf.setTimeZone(TimeZone.getTimeZone("UTC"));
        String start = utcsdf.format(startTime);
        String stop = utcsdf.format(stopTime);
        QueryApi queryApi = influxDBClient.getQueryApi();
        String flux = String.format("from(bucket: \"%s\")\n" +
                "  |> range(start:%s,stop:%s)\n" +
                "  |> filter(fn: (r) => r[\"_measurement\"] == \"Offside\")\n" +
                "  |> group(columns: [\"_measurement\",\"_field\"], mode:\"by\")\n" +
                "  |> sort(columns: [\"_time\",\"keyid\"], desc: true)\n" +
                "  |> limit(n:8)\n" +
                "  |> pivot(rowKey:[\"_time\",\"keyid\"], columnKey: [\"_field\"], valueColumn: \"_value\")",bucketName,start,stop);
        return queryApi.query(flux,Offside.class);
    }

    public List<Offside> newListData(String bucketName,String time)  throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date startTime = sdf.parse(time);
        Date stopTime = sdf.parse(time);
        stopTime.setTime(stopTime.getTime() + 59 * 1000);
        SimpleDateFormat utcsdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
        utcsdf.setTimeZone(TimeZone.getTimeZone("UTC"));
        String start = utcsdf.format(startTime);
        String stop = utcsdf.format(stopTime);
        QueryApi queryApi = influxDBClient.getQueryApi();
        String flux =String.format("from(bucket: \"%s\")\n" +
                "  |> range(start:%s,stop:%s)\n" +
                "  |> filter(fn: (r) => r[\"_measurement\"] == \"Offside\")\n" +
                "  |> last()\n" +
                "  |> pivot(rowKey:[\"_time\"], columnKey: [\"_field\"], valueColumn: \"_value\")",bucketName,start,stop);
        return queryApi.query(flux,Offside.class);
    }

}

//        String flux ="from(bucket: \"my-first\")\n" +
//                "  |> range(start:-24h)\n" +
//                "  |> filter(fn: (r) => r[\"_measurement\"] == \"Offside\")\n" +
//                "  |> pivot(rowKey:[\"_time\"], columnKey: [\"_field\"], valueColumn: \"_value\")\n" +
//                "  |> sort(columns: [\"_time\"], desc: true)\n" +
//                "  |> group(columns:[\"_measurement\"], mode:\"by\")\n" +
//                "  |> limit(n:8)";
